
var qr = require('qr-image'); //生成二维码
var express = require('express');
var app = express();
var template = require('art-template'); //模板
var http = require('http');
var url = require('url');
var async = require('express-async');
var bodyParser = require('body-parser');
var fs = require('fs');
const htmlDocx = require("html-docx-js"); //html转换word
var request   = require('request');
//加载Handlebars模板引擎的服务器端版本hbs模板引擎
//var hbs=require('hbs');
//指定模板文件的后缀名为html
//app.set('view engine','html');
//运行hbs模块
//app.engine('html',hbs.__express);

app.get('/hte',function(req,res){
   res.render('index3') 
});

app.use(express.static('files'));
//app.use(bodyParser());
app.use(bodyParser({limit: '50mb'}));
app.get('/', function (req, res) {
    res.sendfile('./files/index.html');
});

//根据章已经题库获取习题--by luodaishi
app.get('/getHte/:id',function(req, res) {
    //res.redirect('/');
    //res.send('getHte ' + req.params.id);
    res.sendfile('./files/index22.html');
});


app.get('/create_qrcode', function (req, res) {
    var text = req.query.text;
    var size = req.query.size;
    size = size.substring(0, 1);
    try {
        var img = qr.image(text, { size: parseInt(size) });
        res.writeHead(200, { 'Content-Type': 'image/png' });
        img.pipe(res);
    } catch (e) {
        res.writeHead(414, { 'Content-Type': 'text/html' });
        res.end('<h1>414 Request-URI Too Large</h1>');
    }
});

app.post('/test', function (req, res) {
    console.log(req.body.html);
    var time = Date.now();
    var file = "./files/docx/" + time + ".docx";
    var path = "./docx/" + time + ".docx";
    var converted = htmlDocx.asBlob(req.body.html);
    fs.writeFile(file, converted, function (err) {
        //res.send(time + ".docx");
        res.redirect(path);
    });
    //res.send(time + ".docx");
});



// app.get('/FileProgress/Download2/:id', function (req, res) {
//     var path="http://cms.hexstudy.com/FileManage/DownLoad?resourceID="+req.params["id"];
//     res.redirect(path);
// });


app.get('/FileProgress/Download2/:id', function (req, res) {
    var path="http://cms.hexfuture.com/FileManage/DownLoad?resourceID="+req.params["id"];
     var opts={
        url:path
    }
    request.get(opts, function (err, response, body) {
        //var type = response.headers["content-type"];
        //res.writeHead(200,{"Content-Type":type});
        // res.write(body);
        // res.end();
    }).pipe(res);
});

app.listen(3000, function () {
    console.log('Example app listening on port 3000!');
});

